﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartMathLibrary
{
    /// <summary>
    /// This class represents a mathematical square matrix.
    /// </summary>
    [Serializable]
    public class SquareMatrix : Matrix, ISquareMatrix
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="SquareMatrix"/> class.
        /// </summary>
        /// <param name="dimension">The dimension of the square matrix.</param>
        public SquareMatrix(int dimension)
            : base(dimension, dimension)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SquareMatrix"/> class.
        /// </summary>
        /// <param name="matrix">The matrix values of the square matrix. The number of rows and 
        /// columns have to be even.</param>
        public SquareMatrix(double[,] matrix)
            : base(matrix)
        {
            if (!this.IsSquareMatrix)
            {
                throw new ArgumentException("The number of rows and columns have to be even.");
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SquareMatrix"/> class.
        /// </summary>
        /// <param name="matrix">The matrix to clone. The number of rows and columns have to be
        /// even.</param>
        public SquareMatrix(Matrix matrix)
            : base(matrix)
        {
            if (!this.IsSquareMatrix)
            {
                throw new ArgumentException("The number of rows and columns have to be even.");
            }
        }

        /// <summary>
        /// Calculates the trace of the current matrix.
        /// </summary>
        /// <value>The trace of the current matrix.</value>
        public new double Trace
        {
            get
            {
                double result = 0;

                for (int i = 0; i < this.Rows; i++)
                {
                    result += this.GetValueAtPosition(i, i);
                }

                return result;
            }
        }
    }
}